﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HMS_Hospital_Team2._6.DO;

namespace HMS_Hospital_Team2._6.DA
{
    class NhanVienDA
    {
        HMSHospitalEntities context = new HMSHospitalEntities();
        PubicLibDA pubNV = new PubicLibDA();
        //Ham load danh sach nhan vien len datagrid view.
        public DataTable LoadDanhsachNVDA()
        {
            var Nhanvien = from nv in context.NhanViens
                           select new
                           {
                               nv.MaNhanVien,
                               nv.HoVaTen,
                               nv.ChucVu.TenChucVu,
                               nv.DiDong,
                               nv.TrangThai
                           };
            return pubNV.LinqToDataTable(Nhanvien);
        }

        //Ham load chuc vu len combobox.
        public DataTable LoadChucVuDA()
        {
            var Chucvu = from cv in context.ChucVus
                         select new
                         {
                             cv.TenChucVu,
                             cv.MaChucVu
                         };
            return pubNV.LinqToDataTable(Chucvu);
        }

        //Ham them moi nhan vien
        public static void ThemMoiNhanVien(string sManhanvien, string sHovaten, string sdidong, string sdienthoaiban, bool blGioitinh
            , DateTime daNgaysinh, int iMachucvu, string sTendangnhap, string sMatkhau, int iQuyen, bool blTrangThai)
        {
            using (HMSHospitalEntities add = new HMSHospitalEntities())
            {
                NhanVien nhanvien = new NhanVien();
                nhanvien.MaNhanVien = sManhanvien;
                nhanvien.HoVaTen = sHovaten;
                nhanvien.GioiTinh = blGioitinh;
                nhanvien.NgaySinh = daNgaysinh;
                nhanvien.DiDong = sdidong;
                nhanvien.DienThoaiBan = sdienthoaiban;

                nhanvien.MaChucVu = iMachucvu;
                nhanvien.TenDangNhap = sTendangnhap;
                nhanvien.MatKhau = sMatkhau;
                nhanvien.TrangThai = blTrangThai;

                add.AddToNhanViens(nhanvien);
                add.SaveChanges();
            }
            //thêm mới vào bảng phân quyền
            using (HMSHospitalEntities phanquyen = new HMSHospitalEntities())
            {
                PhanQuyen pq = new PhanQuyen();
                pq.MaNhanVien = sManhanvien;
                pq.MaQuyen = iQuyen;
                pq.TrangThai = blTrangThai;
                pq.TenDangNhap = sTendangnhap;
                pq.MatKhau = sMatkhau;

                phanquyen.AddToPhanQuyens(pq);
                phanquyen.SaveChanges();
            }
        }
        //Hàm cập nhật thông tin nhân viên
        public static void CapnhatthongtinNV(string sManhanvien, string sHovaten, string sdidong,string sdienthoaiban, bool blGioitinh
            , DateTime daNgaysinh, int iMachucvu, string sTendangnhap, string sMatkhau, int iQuyen, bool blTrangThai)
        {
            using (HMSHospitalEntities edit = new HMSHospitalEntities())
            {
                var nhanvien = (from nv in edit.NhanViens
                             where nv.MaNhanVien == sManhanvien
                             select nv).First();

                nhanvien.HoVaTen = sHovaten;
                nhanvien.DiDong = sdidong;
                nhanvien.DienThoaiBan = sdidong;
                nhanvien.GioiTinh = blGioitinh;
                nhanvien.NgaySinh = daNgaysinh;
                nhanvien.TenDangNhap = sTendangnhap;
                nhanvien.MatKhau = sMatkhau;
                nhanvien.TrangThai = blTrangThai;

                edit.SaveChanges();
            }
            //cập nhật vào bản phân quyền.
            using (HMSHospitalEntities editquyen = new HMSHospitalEntities())
            {
                var phanquyen = (from pq in editquyen.PhanQuyens
                                where pq.MaNhanVien == sManhanvien
                                select pq).First();
                phanquyen.TrangThai = blTrangThai;
                phanquyen.TenDangNhap = sTendangnhap;
                phanquyen.MatKhau = sMatkhau;

                editquyen.SaveChanges();
            }
        }
        //Hàm xử lý đăng nhập vào hệ thống
        public static bool checkdangnhap(string stendangnhap, string password)
        {
            using (HMSHospitalEntities dangnhap = new HMSHospitalEntities())
            {
                bool check = true;
                var quyen = (from q in dangnhap.PhanQuyens
                            select q);
                foreach (var row in quyen)
                {
                    if (row.TenDangNhap == stendangnhap && row.MatKhau == password)//kiem tra tk va pw co trong database
                    {
                        if (row.TrangThai == false)//kiem tra trang thai
                        {
                            check = true;
                            break;
                        }
                        else
                        {
                            check = false;
                        }
                    }
                    else
                    {
                        check = false;
                    }
                }
                return check;
                
            }
        }

        //Ham load thong tin khi nguoi dung dang nhap vao he thong.
        public static List<phanquyen> Loadthongtindangnhap(string sTendangnhap, string sMatkhau)
        {
            List<phanquyen> thongtin = new List<phanquyen>();
            using (HMSHospitalEntities context = new HMSHospitalEntities())
            {
                var phanquyen = from pquyen in context.PhanQuyens
                                where (pquyen.TenDangNhap == sTendangnhap && pquyen.MatKhau == sMatkhau)
                                select pquyen;

                foreach (var row in phanquyen)
                {
                    phanquyen pq = new phanquyen();

                    pq.MaNhanVien = row.MaNhanVien;
                    pq.MaQuyen = row.MaQuyen;
                    pq.TrangThai = row.TrangThai;
                    pq.TenDangNhap = row.TenDangNhap;
                    pq.MatKhau = row.MatKhau;

                    thongtin.Add(pq);
                }
                return thongtin;
            }
        }

        public static void thaydoimatkhau(string smanhanvien, string smatkhauht, string smatkhaumoi)
        {
            
            using (HMSHospitalEntities changpass = new HMSHospitalEntities())
            {
                var phanquyen = (from pq in changpass.PhanQuyens
                                where pq.MaNhanVien == smanhanvien
                                select pq).First();
                phanquyen.MatKhau = smatkhaumoi;
                changpass.SaveChanges();
            }

            using (HMSHospitalEntities context = new HMSHospitalEntities())
            {
                var phanquyen = (from pq in context.NhanViens
                                 where pq.MaNhanVien == smanhanvien
                                 select pq).First();
                phanquyen.MatKhau = smatkhaumoi;
                context.SaveChanges();
            }
        }

    }
}
